package cn.supermaster.management.web.rest.tool;

import cn.supermaster.management.common.annotation.Log;
import cn.supermaster.management.common.enums.BusinessType;
import cn.supermaster.management.common.page.TableDataInfo;
import cn.supermaster.management.common.response.WebResponse;
import cn.supermaster.management.tool.domain.GenTable;
import cn.supermaster.management.tool.domain.GenTableColumn;
import cn.supermaster.management.web.service.tool.IGenService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.web.rest.tool
 * @ClassName: GenRestController
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/1/13 20:08
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@RestController
@RequestMapping("/tool/gen")
public class GenRestController {

    @Autowired
    private IGenService genService;

    /**
     * 查询代码生成列表
     */
    @RequiresPermissions("tool:gen:list")
    @PostMapping("/list")
    public TableDataInfo genList(GenTable genTable) {
        return genService.queryGenListByCondition(genTable);
    }

    /**
     * 查询数据库列表
     */
    @RequiresPermissions("tool:gen:list")
    @PostMapping("/db/list")
    public TableDataInfo queryTableListByCondition(GenTable genTable) {
        return genService.queryTableListByCondition(genTable);
    }

    /**
     * 查询数据表字段列表
     */
    @RequiresPermissions("tool:gen:list")
    @PostMapping("/column/list")
    public TableDataInfo queryColumnListByCondition(GenTableColumn genTableColumn) {
        return genService.queryColumnListByCondition(genTableColumn);
    }

    /**
     * 导入表结构（保存）
     */
    @RequiresPermissions("tool:gen:list")
    @Log(title = "代码生成", businessType = BusinessType.IMPORT)
    @PostMapping("/importTable")
    public WebResponse doImportTable2Db(String tables) {
        return genService.doImportTable2Db(tables);
    }

    /**
     * 修改保存代码生成业务
     */
    @RequiresPermissions("tool:gen:edit")
    @Log(title = "代码生成", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public WebResponse doModifyGenTable(@Validated GenTable genTable) {
        return genService.doModifyGenTable(genTable);
    }

    /**
     * 删除table
     **/
    @RequiresPermissions("tool:gen:remove")
    @Log(title = "代码生成", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public WebResponse doRemoveGenTable(String ids) {
        return genService.doRemoveGenTable(ids);
    }

    /**
     * 预览代码
     */
    @RequiresPermissions("tool:gen:preview")
    @GetMapping("/preview/{tableId}")
    public WebResponse doCodePreview(@PathVariable("tableId") Long tableId) throws IOException {
        return genService.doCodePreview(tableId);
    }

    /**
     * 生成代码（下载方式）
     */
    @RequiresPermissions("tool:gen:code")
    @Log(title = "代码生成", businessType = BusinessType.GENCODE)
    @GetMapping("/download/{tableName}")
    public void doDownloadCodeWithZip(HttpServletResponse response, @PathVariable("tableName") String tableName) {
         genService.doDownloadCodeWithZip(response, tableName);
    }

    /**
     * 生成代码（自定义路径）
     */
    @RequiresPermissions("tool:gen:code")
    @Log(title = "代码生成", businessType = BusinessType.GENCODE)
    @GetMapping("/genCode/{tableName}")
    public WebResponse doBuildCode(@PathVariable("tableName") String tableName) {
        return genService.doBuildCode(tableName);

    }

    /**
     * 同步数据库
     */
    @RequiresPermissions("tool:gen:edit")
    @Log(title = "代码生成", businessType = BusinessType.UPDATE)
    @GetMapping("/synchDb/{tableName}")
    public WebResponse synchDb(@PathVariable("tableName") String tableName) {
        return genService.doSnchDatebase(tableName);

    }

    /**
     * 批量生成代码
     */
    @RequiresPermissions("tool:gen:code")
    @Log(title = "代码生成", businessType = BusinessType.GENCODE)
    @GetMapping("/batchGenCode")
    public void batchGenCode(HttpServletResponse response, String tables) {
        genService.doBatchBuildCode(response, tables);


    }


}